;; -*- mode: scheme -*-
(define-module (Z572 rde))
(add-to-load-path (format #f "~a/.config/guix/config/" (getenv "HOME")))
(use-modules (guix) (gnu)
             (srfi srfi-1)
             (srfi srfi-26)
             (ice-9 format)
             (ice-9 match)
             (guix transformations))
(use-package-modules linux emacs emacs-xyz guile guile-xyz gnupg rust-apps shellutils)
(use-system-modules keyboard)
(use-modules (Z572)
             (Z572 packages emacs-xyz)
             (Z572 services input-method)
             (gnu home)
             ((guix licenses) #:prefix license:)
             (guix gexp)
             (guix git)
             (gnu home-services)
             (gnu home-services base)
             (gnu home-services gnupg)
             (gnu home-services state)
             (gnu home-services mcron)
             (gnu home-services password-utils)
             (gnu home-services shells)
             (gnu home-services video)
             (gnu home-services shellutils)
             (gnu home-services web-browsers)
             (gnu home-services keyboard)
             (gnu home-services wm)
             (gnu home-services files)
             (gnu home-services mail)
             (gnu home-services fontutils)
             (gnu home-services ssh)
             (gnu home-services emacs)
             (gnu home-services version-control)
             (guix channels)
             (flat packages emacs)
             (guix inferior))

(define-public z-emacs
  (package
    (inherit emacs-pgtk-native-comp)
    (name "z-emacs")
    (version "git.pgtk")
    (source
     (git-checkout
      (url "https://git.savannah.gnu.org/git/emacs.git/")
      (branch "feature/pgtk")))))

(define (make-rust-src rust)
  (package
    (inherit rust)
    (name "rust-src")
    (outputs '("out"))
    (arguments
     (substitute-keyword-arguments (package-arguments rust)
       ((#:phases phases)
        `(modify-phases ,phases
           (delete 'configure)
           (delete 'enable-codegen-tests)
           (delete 'override-jemalloc)
           (delete 'build)
           (delete 'check)
           (delete 'mkdir-prefix-paths)
           (replace 'install
             (lambda* (#:key outputs #:allow-other-keys)
               (let ((src (string-append (assoc-ref outputs "out")
                                         "/lib/rustlib/src/rust")))
                 (mkdir-p src)
                 (copy-recursively "./library" (string-append src "/library"))
                 (copy-recursively "./src" (string-append src "/src")))))
           (delete 'wrap-rustc)
           (delete 'delete-install-logs)))))))

(define home-translate-shell-service-type
  (service-type
   (name 'home-translate-shell)
   (extensions
    (list
     (service-extension
      home-profile-service-type
      (const (list (specification->package "translate-shell"))))
     (service-extension
      home-files-service-type
      (const
       `(("config/translate-shell/init.trans"
          ,(plain-file "translate-shell-conf" ":verbose false\n")))))))
   (default-value #f)
   (description "")))

(define sample-he
  (home-environment
   (packages
    (append
     (list (make-rust-src (specification->package "rust@1.52")))
     (map (compose list specification->package+output symbol->string)
          '(python-language-server
            python
            guile-hall
            ;;guile-bash
            fontmanager
            hicolor-icon-theme
            fzf
            tokei
            sbcl
            neofetch
            rofi
            dunst
            peek
            gnome-screenshot
            rust-analyzer
            libnotify
            rsync
            ;; sbcl-slynk
            tree
            pavucontrol
            arandr
            adwaita-icon-theme
            syncthing
            telegram-desktop
            tintin++
            htop
            ;;isync
            okular
            krita
            hsetroot
            mu
            nethack
            libreoffice
            xmobar
            rust:cargo
            light
            rust:out
            rust:rustfmt
            zip
            tidy
            texmacs
            gettext
            picom
            inkscape
            ccls))))
   (services
    (append (if #f
                (list (service home-zsh-direnv-service-type)
                      (service home-zsh-autosuggestions-service-type)
                      (service home-zsh-service-type
                               (home-zsh-configuration
                                (xdg-flavor? #t)
                                (zshenv (list "\
HISTSIZE=50000
HISTFILE=\"$HOME/.zsh_history\"
SAVEHIST=10000
")))))
                '())
            (list
             (simple-service
              'my-env home-environment-variables-service-type
              `(("GUIX_GITHUB_TOKEN" . ,z-guix-github-token)))
             (simple-service
              'set-brightness-on-login home-run-on-first-login-service-type
              #~(system* #$(file-append light "/bin/light") "-S" "90"))
             (simple-service 'inputrc-config home-files-service-type
                             (list `("inputrc" ,(plain-file
                                                 "inputrc"
                                                 (format #f "~@{~?\n~}"
                                                         "~:@{~a: ~a\n~}"
                                                         '(("C-p" history-search-backward)
                                                           ("C-n" history-search-forward)
                                                           ("M-p" history-search-backward)
                                                           ("M-n" history-search-forward)
                                                           ("C-a" beginning-of-line)
                                                           ("Tab" menu-complete)
                                                           ;;("C-e" eng-of-line)
                                                           ("\\e[5~" beginning-of-history)
                                                           ("\\e[6~" end-of-history)
                                                           ("\\e[3~" delete-char)
                                                           ("\\e[2~" quoted-insert))
                                                         "~:@{set ~a ~:[Off~;On~]~%~}"
                                                         '((colored-stats #t)
                                                           (colored-completion-prefix #t)
                                                           (convert-meta #f)
                                                           (meta-flag #t)
                                                           (expand-tilde #t)
                                                           (input-meta #t)
                                                           (output-meta #t)
                                                           (completion-ignore-case #t)
                                                           ;;(completion-prefix-display-length 3)
                                                           (revert-all-at-newline #t)
                                                           (menu-complete-display-prefix #t)
                                                           (completion-map-case #t)
                                                           (bind-tty-special-chars #f)
                                                           (mark-symlinked-directories #t)
                                                           (skip-completed-text #t)
                                                           (show-all-if-ambiguous #t)
                                                           (show-all-if-unmodified #t)
                                                           (visible-stats #t)))))))
             (home-generic-service
              'gdb
              #:packages (list (specification->package "gdb"))
              #:files (list `("gdbinit"
                              ,(plain-file "gdbinit" "\
set debug-file-directory ~/.guix-profile/lib/debug
set debug-file-directory ~/.guix-home/profile/lib/debug
set auto-load safe-path /gnu/store/*/lib\n"))))
             ;; (simple-service
             ;;       'gdb home-files-service-type
             ;;       (list `("gdbinit"
             ;;               ,(plain-file "gdbinit" "\
             ;; # Tell GDB where to look for separate debugging files.
             ;; set debug-file-directory ~/.guix-profile/lib/debug
             ;; set debug-file-directory ~/.guix-home/profile/lib/debug
             ;; # Authorize extensions found in the store, such as the
             ;; # pretty-printers of libstdc++.
             ;; set auto-load safe-path /gnu/store/*/lib\n"))))
             ;; (simple-service
             ;;  'translate-shell-config home-files-service-type
             ;;  (list `("config/translate-shell/init.trans"
             ;;          ,(plain-file "translate-shell-conf" ":verbose false\n"))))
             ;; (service home-notmuch-service-type
             ;;          (home-notmuch-configuration
             ;;           ;;(xdg-flavor? #f)
             ;;           (config `((user ((name . "Zheng Junjie")
             ;;                            (primary_email . ,z-email)
             ;;                            (other_email
             ;;                             . ("zhengjunjie@yandex.com"
             ;;                                "z2j2572@gmail.com"))))
             ;;                     (database ((path . ,(string-append
             ;;                                          (getenv "HOME") "/Mail"))))
             ;;                     (new ((tags . (index unread))))
             ;;                     (search ((exclude_tags . (deleted spam))))
             ;;                     (maildir ((synchronize_flags . true)))))))
             (service home-translate-shell-service-type)
             (service home-mpv-service-type
                      (home-mpv-configuration
                       (default-options
                         `((vo . "x11,drm")))))
             (simple-service
              'mail-signature home-files-service-type
              (list `("signature"
                      ,(plain-file "signature" z-email-signature))))
             (service home-state-service-type
                      (list (state-git (string-append (getenv "HOME") "/gits/z-guix-bot")
                                       "https://github.com/Z572/z-guix-bot")
                            (state-git (string-append (getenv "HOME") "/gits/rde")
                                       "https://github.com/Z572/rde")))
             (service home-isync-service-type
                      (home-isync-configuration
                       (xdg-flavor? #f)
                       (config
                        `((IMAPAccount qq)
                          (Host imap.qq.com)
                          (User ,z-email)
                          (Pass ,z-email-password)
                          (Port "993")
                          (SSLType IMAPS)
                          (SSLVersion TLSv1.2)
                          (AuthMechs LOGIN)
                          (CertificateFile /etc/ssl/certs/ca-certificates.crt)
                          ()
                          (IMAPStore qq-remote)
                          (Account qq)
                          ()
                          (MaildirStore qq-local)
                          (SubFolders Verbatim)
                          (Path ~/Mail)
                          (Inbox ~/Mail/qq/INBOX)
                          ()
                          (Channel qq)
                          (Far :qq-remote:)
                          (Near :qq-local:)
                          ()
                          (IMAPAccount yandex)
                          (Host imap.yandex.com)
                          (User zhengjunjie@yandex.com)
                          (Pass ,z-yandex-email-password)
                          (SSLType IMAPS)
                          (CertificateFile /etc/ssl/certs/ca-certificates.crt)
                          ()
                          (IMAPStore yandex-remote)
                          (Account yandex)
                          ()
                          (MaildirStore yandex-local)
                          (SubFolders Verbatim)
                          (Path ~/Mail/yandex)
                          (Inbox ~/Mail/yandex/INBOX)
                          ()
                          (Channel yandex)
                          (Far :yandex-remote:)
                          (Near :yandex-local:)))))
             (service home-sway-service-type
                      (home-sway-configuration
                       (config
                        `(;; (xwayland disable
                          ;;           ;; enable
                          ;;           )
                          (include ,(file-append (specification->package "sway") "/etc/sway/config"))
                          (set $mod Mod4)
                          (set $left h)
                          (set $down j)
                          (set $up k)
                          (set $right l)
                          (hide_edge_borders smart)
                          (title_align center)
                          (default_border pixel 0)
                          (bindsym $mod+Ctrl+Return exec emacsclient -c --eval "'(vterm)'")
                          (bindsym $mod+F8 exec emacs)
                          (bindsym $mod+z exec --no-startup-id
                                   ,(file-append (specification->package "wofi")
                                                 "/bin/wofi --show drun"))
                          ;;(bindsym $mod+Shift+q kill)
                          ;;(bindsym $mod+Ctrl+q exit)
                          (bindsym XF86MonBrightnessUp   exec --no-startup-id
                                   ,(file-append light "/bin/light -A 10"))
                          (bindsym XF86MonBrightnessDown exec --no-startup-id
                                   ,(file-append light "/bin/light -U 10"))))))
             (service home-mcron-service-type
                      (home-mcron-configuration
                       (jobs
                        (list
                         #~(job
                            '(next-minute
                              (range 0 60 10))
                            (lambda _
                              (let ((home-program
                                     (lambda (name)
                                       (string-append #$(getenv "HOME")
                                                      "/.guix-home/profile/bin/" name))))
                                (setlocale LC_ALL "zh_CN.UTF-8")
                                (format #t "[~a]~%" (strftime "%c" (localtime (current-time))))
                                (and (= (system* (home-program "mbsync")
                                                 "-a") 0)
                                     (system* (home-program "mu")
                                              "index")))))))))
             (service home-ssh-service-type
                      (home-ssh-configuration
                       (extra-config
                        (list (ssh-host
                               (host "guixcn")
                               (options
                                '((hostname . "shanghai.guix.org.cn")
                                  (user . "Z572")
                                  (port . 22)
                                  (identity-file
                                   "~/.ssh/Z572_guixcn_ed25519"))))))))
             (service home-git-service-type
                      (home-git-configuration
                       (ignore '(".envrc"))
                       (config
                        `((pull ((rebase . #t)))
                          (user ((name . ,z-name)
                                 (email . ,z-email)))
                          (gpg ((program . ,(file-append gnupg "/bin/gpg"))))
                          (github ((user . ,z-name)))
                          (gitlab ((user . ,z-name)))))))
             (service (service-type
                       (name 'home-bash-direnv)
                       (extensions
                        (list
                         (service-extension
                          home-profile-service-type
                          (const (list direnv)))
                         (service-extension
                          home-bash-service-type
                          (const
                           (home-bash-extension
                            (bashrc (list
                                     #~(format #f "if [ -z \"$GUIX_ENVIRONMENT\" ];then
  eval \"$(~a hook bash)\"
fi"
                                               #$(file-append direnv "/bin/direnv")))))))))
                       (default-value #f)
                       (description "Enables @code{direnv} for @code{bash}.  Adds hook to
@file{.bashrc} and installs a package in the profile.")))
             (service home-fcitx5-server-type)
             (service (service-type
                       (name 'home-bash-zoxide)
                       (extensions
                        (list
                         (service-extension
                          home-profile-service-type
                          (const (list zoxide)))
                         (service-extension
                          home-bash-service-type
                          (const
                           (home-bash-extension
                            (bashrc (list
                                     #~(begin
                                         (use-modules (ice-9 format))
                                         (format #f "alias zoxide=~a~%eval ~S"
                                                 #$(file-append zoxide "/bin/zoxide")
                                                 "$(zoxide init bash)")))))))))
                       (default-value #f)
                       (description "Enables @code{zoxide} for @code{bash}.  Adds hook to
@file{.bashrc} and installs a package in the profile.")))

             (service home-icecat-service-type
                      (home-icecat-configuration
                       (profiles
                        (list (icecat-profile
                               (default? #t)
                               (name "default")
                               (id 0)
                               (settings '((browser.urlbar.shortcuts.history . #t)
                                           (browser.fullscreen.autohide . #t)
                                           (toolkit.legacyUserProfileCustomizations.stylesheets . #t)
                                           (svg.context-properties.content.enabled . #t)))
                               ;; (user-chrome "\
                               ;; #TabsToolbar { visibility: collapse !important; }")
                               (user-content "\
:root{ scrollbar-width: none !important; }"))
                              (icecat-profile
                               (default? #f)
                               (name "github")
                               (id 1)
                               (settings '((browser.urlbar.shortcuts.bookmarks . #f)
                                           (browser.fullscreen.autohide . #t))))))))
             (service home-bash-service-type
                      (home-bash-configuration
                       (guix-defaults? #f)
                       (bashrc '("\
export SHELL

if [[ $- != *i* ]]
then
    # We are being invoked from a non-interactive shell.  If this
    # is an SSH session (as in \"ssh host command\"), source
    # /etc/profile so we get PATH and other essential variables.
    [[ -n \"$SSH_CLIENT\" ]] && source /etc/profile

    # Don't do anything else.
    return
fi

# Source the system-wide file.
source /etc/bashrc

if [ -n \"$GUIX_ENVIRONMENT\" ]
then
    PS1='\\u@\\h \\w [env]\\$ '
else
    PS1='\\u@\\h \\w\\$ '
fi

HISTCONTROL=ignoreboth
shopt -s histappend
HISTSIZE=5000
HISTFILEIZE=2000
shopt -s autocd
shopt -s checkwinsize
"))))
             (service home-emacs-service-type
                      (home-emacs-configuration
                       (package z-emacs)
                       (server-mode? #t)
                       (elisp-packages
                        (list emacs-leaf-keywords
                              emacs-avy
                              emacs-blackout
                              emacs-ctrlf
                              emacs-debbugs

                              ;;emacs-meow
                              emacs-xr
                              emacs-dimmer
                              emacs-citre
                              emacs-easy-escape
                              emacs-diredfl
                              emacs-doom-modeline
                              emacs-eglot
                              emacs-emojify
                              emacs-envrc
                              emacs-flycheck
                              emacs-flycheck-guile
                              emacs-guix
                              emacs-highlight-quoted
                              ;;emacs-cmake-mode
                              emacs-prescient
                              emacs-helpful
                              emacs-highlight-defined
                              emacs-hl-todo
                              emacs-macrostep
                              emacs-forge
                              emacs-magit
                              emacs-calfw
                              emacs-magit-todos
                              emacs-symbol-overlay
                              emacs-telega
                              emacs-vterm
                              emacs-selectrum
                              emacs-no-littering
                              emacs-nov-el
                              emacs-bing-dict
                              emacs-olivetti
                              emacs-page-break-lines
                              emacs-paren-face
                              emacs-paredit
                              emacs-go-mode
                              emacs-pdf-tools
                              emacs-repology
                              emacs-winum
                              emacs-ebdb
                              emacs-restart-emacs
                              emacs-rime
                              emacs-rust-mode
                              emacs-mu4e-patch
                              emacs-mu4e-jump-to-list
                              emacs-cider
                              emacs-haskell-mode
                              emacs-nix-mode
                              emacs-shackle
                              emacs-sly
                              emacs-web-mode
                              emacs-which-key
                              emacs-ws-butler
                              emacs-yasnippet
                              emacs-yasnippet-snippets
                              emacs-aggressive-indent))))
             (service home-gnupg-service-type
                      (home-gnupg-configuration
                       (gpg-config
                        (home-gpg-configuration
                         (extra-config
                          '((cert-digest-algo . "SHA256")
                            (default-preference-list
                              . ("SHA512"
                                 "SHA384"
                                 "SHA256"
                                 "SHA224"
                                 "AES256"
                                 "AES192"
                                 "Uncompressed"))
                            (with-fingerprint? . #t)))))
                       (gpg-agent-config
                        (home-gpg-agent-configuration
                         (ssh-agent? #t)
                         (pinentry-flavor 'emacs)
                         (extra-options '("--verbose"))
                         (extra-config
                          '((max-cache-ttl . 86400)))))))
             (service home-password-store-service-type
                      (home-password-store-configuration
                       (browserpass-native? #t)))
             (service home-keyboard-service-type
                      (keyboard-layout
                       "us" #:options
                       '("ctrl:nocaps"
                         "shift:both_capslock"))))))))

sample-he
